﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using XiaoGang.Soft.XGWMS.COM;

namespace XiaoGang.Soft.XGWMS.UI
{
    public partial class M008_STOREROOM : FormBase
    {
        #region 变量定义

        private XGWMS.BLL.BLL_M008_STOREROOM bll_M008_STOREROOM;
        //private bool blFormLoad = false;
        private MODEL.MOD_M008_STOREROOM model;
        /// <summary>
        /// 仓库
        /// </summary>
        private const string dataName = "仓库";
        private bool isInit = true;

        #endregion

        #region 窗口初始化

        public M008_STOREROOM()
        {
            InitializeComponent();
            model = new XiaoGang.Soft.XGWMS.MODEL.MOD_M008_STOREROOM();
            bll_M008_STOREROOM = new XiaoGang.Soft.XGWMS.BLL.BLL_M008_STOREROOM();
        }

        /// <summary>	
        /// <p><b>方法名称</b>SetTabIndex</p>	
        /// <p><b>概要説明	:</b>画面フォーカスの遷移順の設定</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/01</p>	
        /// </summary>
        private void SetTabIndex()
        {
            int intIndex = 0;           
            txtSTOREROOM_ID.TabIndex = intIndex; intIndex++;
            txtSTOREROOM_NM.TabIndex = intIndex; intIndex++;
            txtSTOREROOM_PHONE.TabIndex = intIndex; intIndex++;
            txtSTOREROOM_ADDRESS.TabIndex = intIndex; intIndex++;
            cmbSTOREROOM_MANAGER.TabIndex = intIndex; intIndex++;
            ckbEnable.TabIndex = intIndex; intIndex++;
            ckbDisEnable.TabIndex = intIndex; intIndex++;
            txtMEMO.TabIndex = intIndex; intIndex++;      
            btnAdd.TabIndex = intIndex; intIndex++;
            btnExit.TabIndex = intIndex; intIndex++;
        }

        /// <summary>	
        /// <p><b>方法名称</b>ClearControl</p>	
        /// <p><b>概要説明	:</b>画面上の全項目をオールクリアする</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/01</p>	
        /// </summary>
        private void ClearControl()
        {
            txtSTOREROOM_ID.Text = "";
            txtSTOREROOM_NM.Text = "";
            txtSTOREROOM_PHONE.Text = "";
            txtSTOREROOM_ADDRESS.Text = "";
            cmbSTOREROOM_MANAGER.SelectedIndex = 0;
            ckbEnable.Checked = true;
            ckbDisEnable.Checked = false;
            txtMEMO.Text = "";
        }

        /// <summary>	
        /// <p><b>方法名称</b>SetEnable</p>	
        /// <p><b>概要説明  :</b>画面モードのセト</p>
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/08/06</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>	
        private void SetEnable()
        {
            grpStroeRoomInfo.Enabled = true;
            txtSTOREROOM_ID.Enabled = true;
            txtSTOREROOM_NM.Enabled = true;
            txtSTOREROOM_PHONE.Enabled = true;
            txtSTOREROOM_ADDRESS.Enabled = true;
            cmbSTOREROOM_MANAGER.Enabled = true;
            ckbEnable.Enabled = true;
            ckbDisEnable.Enabled = true;
            txtMEMO.Enabled = true;
            grpSearchResult.Enabled = true;
            grdmentStoreRoom.Enabled = true;
            btnAdd.Enabled = true;
            btnExit.Enabled = true;
        }

        /// <summary>	
        /// <p><b>方法名称</b>InitialGridView</p>	
        /// <p><b>概要説明	:</b>窗口初始化</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/01</p>	
        /// </summary>
        private void InitialGridView()
        {
            grdmentStoreRoom.AllowUserToAddRows = false;
            grdmentStoreRoom.AllowUserToResizeColumns = true;
            grdmentStoreRoom.AllowUserToDeleteRows = false;
            grdmentStoreRoom.AllowUserToResizeRows = false;
            grdmentStoreRoom.ColumnHeadersHeight = 30;
            grdmentStoreRoom.ColumnHeadersVisible = true;
            grdmentStoreRoom.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            grdmentStoreRoom.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            grdmentStoreRoom.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Raised;
            grdmentStoreRoom.ReadOnly = true;
            grdmentStoreRoom.RowHeadersVisible = false;
            grdmentStoreRoom.RowTemplate.Resizable = DataGridViewTriState.False;
            grdmentStoreRoom.RowTemplate.Height = 25;
            grdmentStoreRoom.RowTemplate.ReadOnly = true;
            grdmentStoreRoom.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            grdmentStoreRoom.ScrollBars = ScrollBars.Both;
            grdmentStoreRoom.TabStop = false;

            grdmentStoreRoom.Columns.Add("NO.", "NO.");
            grdmentStoreRoom.Columns.Add("STOREROOM_ID", "仓库编号");
            grdmentStoreRoom.Columns.Add("STOREROOM_NM", "仓库名称");
            grdmentStoreRoom.Columns["STOREROOM_NM"].Width = 150;
            grdmentStoreRoom.Columns["STOREROOM_NM"].Frozen = true;
            DataGridViewButtonColumn btnDelete = new DataGridViewButtonColumn() { Text = "删除", Width = 80, HeaderText = "删除", SortMode = DataGridViewColumnSortMode.NotSortable, Resizable = DataGridViewTriState.False, Frozen =true};
            grdmentStoreRoom.Columns.Add(btnDelete);
            grdmentStoreRoom.Columns.Add("STOREROOM_ADDRESS", "地址");
            grdmentStoreRoom.Columns.Add("STOREROOM_PHONE", "电话");
            grdmentStoreRoom.Columns.Add("STOREROOM_MANAGER", "管理者ID");
            grdmentStoreRoom.Columns.Add("STOREROOM_MANAGER_NM", "管理者名称");
            grdmentStoreRoom.Columns.Add("STOREROOM_STATE", "状态");
            grdmentStoreRoom.Columns.Add("MEMO", "备注");

            grdmentStoreRoom.Columns["NO."].Width = 50;
            grdmentStoreRoom.Columns["NO."].Frozen = true;
            grdmentStoreRoom.Columns["NO."].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            grdmentStoreRoom.Columns["NO."].Resizable = DataGridViewTriState.False;
            grdmentStoreRoom.Columns["STOREROOM_ID"].Width = 150;
            grdmentStoreRoom.Columns["STOREROOM_ID"].Resizable = DataGridViewTriState.False;
            
            grdmentStoreRoom.Columns["STOREROOM_ADDRESS"].Width = 200;
            grdmentStoreRoom.Columns["STOREROOM_PHONE"].Width = 150;
            grdmentStoreRoom.Columns["STOREROOM_MANAGER"].Width = 0;
            grdmentStoreRoom.Columns["STOREROOM_MANAGER"].Visible = false;
            grdmentStoreRoom.Columns["STOREROOM_MANAGER_NM"].Width = 150;            
            grdmentStoreRoom.Columns["STOREROOM_STATE"].Width = 80;
            grdmentStoreRoom.Columns["MEMO"].Width = 300;
        }

        /// <summary>
        /// <p><b>方法名称</b>InitStoreRoomManger</p>	
        /// <p><b>概要説明	:</b>Control cmbSTOREROOM_MANAGER の初期化する</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/01</p>	
        /// </summary>
        private void InitComboBox()
        {
            Method.CmbDataBound("USER_ID", cmbSTOREROOM_MANAGER);
            cmbSTOREROOM_MANAGER.SelectedIndex = 0;
        }

        /// <summary>
        /// <p><b>方法名称</b>InitProcess</p>	
        /// <p><b>概要説明	:</b>窗口初始化</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/01</p>	
        /// </summary>
        private void InitProcess()
        {
            SetTabIndex();            
            SetEnable();
            InitialGridView();
            InitComboBox();
            ClearControl();
            isInit = false;
        }

        #endregion

        #region 方法

        /// <summary>	
        /// <p><b>方法名称</b>ShowGrid</p>	
        /// <p><b>概要説明  :</b>検索結果をGridに表示する</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/01</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>	
        /// <param type="MOD_M008_STOREROOM" param="model"></param>	
        private void BindGridData()
        {
            if (isInit)
            {
                return;
            }
            grdmentStoreRoom.Rows.Clear();

            model.MEMO = txtMEMO.Text.Trim();
            model.STOREROOM_ADDRESS = txtSTOREROOM_ADDRESS.Text.Trim();
            model.STOREROOM_ID = txtSTOREROOM_ID.Text.Trim();
            if (cmbSTOREROOM_MANAGER.Items.Count > 0 && cmbSTOREROOM_MANAGER.SelectedIndex >= 0)
            {
                model.STOREROOM_MANAGER = cmbSTOREROOM_MANAGER.SelectedValue.ToString();
            }
            else
            {
                model.STOREROOM_MANAGER = "";
            }
            model.STOREROOM_NM = txtSTOREROOM_NM.Text.Trim();
            model.STOREROOM_PHONE = txtSTOREROOM_PHONE.Text.Trim();
            if (ckbEnable.Checked && ckbDisEnable.Checked )
            {
                model.STOREROOM_STATE = "2";
            }
            else if (ckbEnable.Checked == true)
            {
                model.STOREROOM_STATE = "0";
            }
            else if (ckbDisEnable.Checked == true)
            {
                model.STOREROOM_STATE = "1";
            }
            else
            {
                model.STOREROOM_STATE = "3";
            }
            ReturnValue rvSearchResult = bll_M008_STOREROOM.SearchProcess(model);
            if (rvSearchResult.IsSuccess == false)
            {
                Method.MessageShow(rvSearchResult.ErrorID,dataName);
                return;
            }
            if (0 == rvSearchResult.ResultDataSet.Tables[0].Rows.Count)
            {
                GlobalVal.glostrMsg = "W11008";
                grpSearchResult.Text = "检索结果";
                return;
            }
            for (int i = 0; i < rvSearchResult.ResultDataSet.Tables[0].Rows.Count; i++)
            {
                grdmentStoreRoom.Rows.Insert(grdmentStoreRoom.Rows.Count,
                    (i + 1).ToString(),
                    rvSearchResult.ResultDataSet.Tables[0].Rows[i]["STOREROOM_ID"].ToString().Trim(),
                    rvSearchResult.ResultDataSet.Tables[0].Rows[i]["STOREROOM_NM"].ToString().Trim(),
                    "删除",
                    rvSearchResult.ResultDataSet.Tables[0].Rows[i]["STOREROOM_ADDRESS"].ToString().Trim(),
                    rvSearchResult.ResultDataSet.Tables[0].Rows[i]["STOREROOM_PHONE"].ToString().Trim(),
                    rvSearchResult.ResultDataSet.Tables[0].Rows[i]["STOREROOM_MANAGER"].ToString().Trim(),
                    rvSearchResult.ResultDataSet.Tables[0].Rows[i]["STOREROOM_MANAGER_NM"].ToString().Trim(),
                    rvSearchResult.ResultDataSet.Tables[0].Rows[i]["STOREROOM_STATE"].ToString().Trim(),
                    rvSearchResult.ResultDataSet.Tables[0].Rows[i]["MEMO"].ToString().Trim());
                if (txtSTOREROOM_ID.Text.Trim() == rvSearchResult.ResultDataSet.Tables[0].Rows[i]["STOREROOM_ID"].ToString().Trim())
                {
                    grdmentStoreRoom.Rows[0].Selected = false;
                    grdmentStoreRoom.Rows[i].Selected = true;
                    grdmentStoreRoom.FirstDisplayedScrollingRowIndex = i * (i / 15);
                }
            }

            grpSearchResult.Text = String.Format("检索结果  共{0}件", grdmentStoreRoom.Rows.Count);
        }

        /// <summary>
        /// 检查仓库是否正在使用
        /// </summary>
        /// <param name="strGoodsGroupID"></param>
        /// <returns>true:使用或发生错误 false:未使用</returns>
        protected bool IsStoreRoomUsed(string StoreRoomID)
        {
            ReturnValue rvReturnValue = bll_M008_STOREROOM.StorageRoomDeleteCheck(StoreRoomID);
            if (rvReturnValue.IsSuccess)
            {
                if (rvReturnValue.ResultDataSet.Tables[0].Rows.Count > 0)
                {
                    Method.MessageShow("W11D01", dataName);//本@正在使用!不能删除!
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                Method.MessageShow(rvReturnValue.ErrorID, dataName);
                return true;
            }
        } 

        #endregion        

        #region 事件

        /// <summary>
        /// 单击删除按钮进行删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void grdmentStoreRoom_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.Cursor == Cursors.WaitCursor)
            {
                return;
            }
            int intColumnIndex = e.ColumnIndex;
            //删除
            if (intColumnIndex == 3 && e.RowIndex != -1)
            {
                //检查该分类是否正在被使用
                if (IsStoreRoomUsed(grdmentStoreRoom.CurrentRow.Cells["STOREROOM_ID"].Value.ToString()))
                {
                    this.Cursor = Cursors.Default;
                    return;
                }
                
                //物理彻底删除
                if (Method.MessageShow("Q22007", dataName) == DialogResult.Yes)//确定要删除该条数据吗?
                {
                    this.Cursor = Cursors.WaitCursor;
                    try
                    {
                        ReturnValue result = bll_M008_STOREROOM.PhysicalDeleteProcess(grdmentStoreRoom.CurrentRow.Cells["STOREROOM_ID"].Value.ToString());
                        if (result.IsSuccess)
                        {
                            BindGridData();
                        }
                        else
                        {
                            Method.MessageShow(result.ErrorID, dataName);
                        }
                    }
                    catch
                    {
                        Method.MessageShow("E11004", dataName);//An unknown error process!
                    }
                    finally
                    {
                        this.Cursor = Cursors.Default;
                    }
                }
            }
        }
        
        private void M008_STOREROOM_Load(object sender, EventArgs e)
        {            
            InitProcess();
            BindGridData();
            txtSTOREROOM_ID.Focus();
        }

        private void AllTextBox_KeyPrecess(object sender, KeyPressEventArgs e)
        {

        }

        private void AllTextBox_GotFocus(object sender, EventArgs e)
        {
            string strValue = ((TextBox)sender).Text.Trim();
            switch (((TextBox)sender).Name)
            {
                case "txtSTOREROOM_ID":
                    ((TextBox)sender).SelectAll();
                    ((TextBox)sender).ImeMode = ImeMode.Disable;
                    break;
                case "txtSTOREROOM_NM":
                    ((TextBox)sender).SelectAll();
                    ((TextBox)sender).ImeMode = ImeMode.Hiragana;
                    break;
                case "txtMEMO":
                    ((TextBox)sender).ImeMode = ImeMode.Hiragana;
                    break;
                case "txtSTOREROOM_ADDRESS":
                    ((TextBox)sender).SelectAll();
                    ((TextBox)sender).ImeMode = ImeMode.Hiragana;
                    break;
                case "txtSTOREROOM_PHONE":
                    ((TextBox)sender).SelectAll();
                    ((TextBox)sender).ImeMode = ImeMode.Disable;
                    break;              
            }
        }

        private void AllButton_Click(object sender, EventArgs e)
        {
            if (Cursor.Current == Cursors.WaitCursor)
            {
                return;
            }
            Cursor.Current = Cursors.WaitCursor;
            
            string strButtonName = ((Button)sender).Name;

            switch (strButtonName)
            {

                #region btnAdd
                case "btnAdd":
                    {
                        M008StoreRoomEdit from=new M008StoreRoomEdit();

                        if (from.ShowDialog() == DialogResult.OK)
                        {
                            BindGridData();
                        }
                        break;
                    }
                #endregion
                
                #region btnExit
                case "btnExit":
                    this.Close();
                    break;
                #endregion

                #region btnClear
                case "btnClear":
                    {
                        ClearControl();                       
                        txtSTOREROOM_ID.Focus();
                        grpSearchResult.Text = "检索结果";
                    }
                    break;
                #endregion
            }

            Cursor.Current = Cursors.Default;
        }

        private void M008_STOREROOM_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                
                case Keys.F5:
                    AllButton_Click(btnAdd, e);
                    break;
                case Keys.Escape:
                    AllButton_Click(btnExit, e);
                    break;
            }          
        }

        //private void M008_STOREROOM_Activated(object sender, EventArgs e)
        //{
        //    if (!blFormLoad)
        //    {
        //        string strValue = cmbSTOREROOM_MANAGER.SelectedValue.ToString();
        //        Method.CmbDataBound("USER_ID", cmbSTOREROOM_MANAGER);
        //        foreach (System.Data.DataRowView dr in this.cmbSTOREROOM_MANAGER.Items)
        //        {
        //            if (strValue == dr["USER_ID"].ToString().Trim())
        //            {
        //                cmbSTOREROOM_MANAGER.SelectedValue = strValue;
        //                return;
        //            }
        //        }
        //        cmbSTOREROOM_MANAGER.SelectedIndex = 0;
        //    }
        //    blFormLoad = false;
        //}

        /// <summary>
        /// 查询条件改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void QueryChanged(object sender, EventArgs e)
        {
            if (this.Cursor == Cursors.WaitCursor)
            {
                return;
            }
            BindGridData();
        }

        /// <summary>
        /// 双击进行编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void grdmentStoreRoom_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.Cursor == Cursors.WaitCursor)
            {
                return;
            }
            if (0 == grdmentStoreRoom.Rows.Count || e.RowIndex == -1)
            {
                return;
            }
            string strStoreRoomID = grdmentStoreRoom.CurrentRow.Cells["STOREROOM_ID"].Value.ToString();
            string strStoreRoomName = grdmentStoreRoom.CurrentRow.Cells["STOREROOM_NM"].Value.ToString();
            string strStoreRoomPhone = grdmentStoreRoom.CurrentRow.Cells["STOREROOM_PHONE"].Value.ToString();
            string strStoreRoomAddress = grdmentStoreRoom.CurrentRow.Cells["STOREROOM_ADDRESS"].Value.ToString();
            string strStoreRoomManager = grdmentStoreRoom.CurrentRow.Cells["STOREROOM_MANAGER"].Value.ToString();
            string strStoreRoomState = grdmentStoreRoom.CurrentRow.Cells["STOREROOM_STATE"].Value.ToString();
            string strStoreRoomMemo = grdmentStoreRoom.CurrentRow.Cells["MEMO"].Value.ToString();

            if (new M008StoreRoomEdit(strStoreRoomID,strStoreRoomName,strStoreRoomPhone,strStoreRoomAddress,strStoreRoomManager,strStoreRoomState,strStoreRoomMemo).ShowDialog() == DialogResult.OK)
            {
                //刷新Grid数据
                BindGridData();
            }
        }

        #endregion
    }
}